package org.py.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.py.common.MessageCode;
import org.py.common.MessageObject;
import org.py.common.MessageUtils;
import org.py.entity.Userlogin;
import org.py.entity.Users;
import org.py.jwt.SystemConstant;
import org.py.jwt.TokenUtil;
import org.py.mapper.UsersMapper;
import org.py.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anyi
 * @since 2025-03-14
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public String getAllUsers() {
        List<Users> users = usersMapper.selectList(null);
        MessageObject messageObject = new MessageObject();
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent(users);
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String getUserById(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Users.class);
        Users users = (Users) messageObject.getContent();
        QueryWrapper<Users>queryWrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(users)){
            queryWrapper.eq("id",users.getId());
        }
        Users users1 = usersMapper.selectOne(queryWrapper);
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent(users1);
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String createUser(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Users.class);
        Users content = (Users) messageObject.getContent();
        QueryWrapper<Users> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",content.getUsername());
        Users users = usersMapper.selectOne(queryWrapper);
        if (ObjectUtils.isNotNull(users)){
            messageObject.setCode(MessageCode.M1002);
            messageObject.setContent("用户已存在");
            return MessageUtils.ObjectTojson(messageObject);
        }
        int insert = usersMapper.insert(content);
        if (insert<0){
            messageObject.setCode(MessageCode.M1001);
            messageObject.setContent("新增失败");
            return MessageUtils.ObjectTojson(messageObject);
        }
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent("新增成功");
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String updateUser(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Users.class);
        Users content = (Users) messageObject.getContent();
        UpdateWrapper<Users> updateWrapper=new UpdateWrapper<>();
        if (ObjectUtils.isNotEmpty(content)){
            if (content.getId()!=null){
                updateWrapper.eq("id",content.getId());
            }
            if (StringUtils.isNotBlank(content.getUsername())){
                updateWrapper.set("username",content.getUsername());
            }
            if (StringUtils.isNotBlank(content.getPassWord())){
                updateWrapper.set("pass_word",content.getPassWord());
            }
        }
        int update = usersMapper.update(updateWrapper);
        if (update<0){
            messageObject.setCode(MessageCode.M1001);
            messageObject.setContent("修改失败");
            return MessageUtils.ObjectTojson(messageObject);
        }
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent("修改成功");
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String deleteUser(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Users.class);
        Users content = (Users) messageObject.getContent();
        int i = usersMapper.deleteById(content.getId());
        if (i<0){
            messageObject.setCode(MessageCode.M1001);
            messageObject.setContent("删除失败");
            return MessageUtils.ObjectTojson(messageObject);
        }
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent("删除成功");
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String login(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Userlogin.class);
        Userlogin userlogin = (Userlogin) messageObject.getContent();
        if(userlogin!=null){
            if(StringUtils.isNotEmpty(userlogin.getUsername())) {
                QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("username", userlogin.getUsername());
                Users userloginreep = this.getOne(queryWrapper);
                if(ObjectUtils.isNotNull(userloginreep)){
                    if(userloginreep.getPassWord().equals(userlogin.getPassword())){
                        String token= TokenUtil.createJWT(userloginreep.getId().toString(),userlogin.getMachinecode(), SystemConstant.JWT_TTL_HS);
                        if(!StringUtils.isEmpty(token)) {
                            Userlogin backlogin_rep = new Userlogin();
                            backlogin_rep.setUsername(userloginreep.getUsername());
                            backlogin_rep.setTypes(userloginreep.getType());
                            backlogin_rep.setToken(token);
                            backlogin_rep.setUserid(userloginreep.getId());
                            backlogin_rep.setAuthority(userloginreep.getAuthority());
                            backlogin_rep.setUseridback(userloginreep.getUseridback());
                            messageObject.setCode(MessageCode.M1000);
                            messageObject.setContent(backlogin_rep);
                        }else{
                            messageObject.setCode(MessageCode.M1002);
                            messageObject.setContent("您输入的数据生成用户唯一标识出现错误");
                        }
                    }else{
                        messageObject.setCode(MessageCode.M1001);
                        messageObject.setContent("您输入的密码不正确");
                    }
                }else{
                    messageObject.setCode(MessageCode.M1001);
                    messageObject.setContent("您输入的用户名不存在");
                }
            }else{
                messageObject.setCode(MessageCode.M1001);
                messageObject.setContent("您输入的用户名为空");
            }
        }else{
            messageObject.setCode(MessageCode.M1001);
            messageObject.setContent("您输入的用户名和密码为空");
        }
        return MessageUtils.ObjectTojson(messageObject);
    }
}
